package com.graduation.rescuematerial.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.rescuematerial.dto.StatisticsDTO;
import com.graduation.rescuematerial.mapper.CompanyMaterialMapper;
import com.graduation.rescuematerial.mapper.MaterialMapper;
import com.graduation.rescuematerial.pojo.CompanyMaterial;
import com.graduation.rescuematerial.pojo.Material;
import com.graduation.rescuematerial.service.MaterialService;
import com.graduation.rescuematerial.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CaiShuangLian
 * @since 2022-04-22
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired 
    private MaterialMapper materialMapper;
    @Autowired
    private CompanyMaterialMapper companyMaterialMapper;
    /**
     * 查询可申请的物资列表（库存大于零的物资）
     * @return
     */
    @Override
    public RespBean getApplicableMaterial() {
        return RespBean.success(materialMapper.getApplicableMaterial());
    }

    /**
     * 获取本部门物资统计
     * @param companyName
     * @return
     */
    @Override
    public RespBean getStatistics(String companyName) {
        return RespBean.success(materialMapper.getStatistics(companyName));
    }

    @Override
    public RespBean fuzzyQuery(String companyName, String key) {
        HashMap<String,String> map=new HashMap<>();
        map.put("companyName",companyName);
        map.put("key",key);
        return RespBean.success(materialMapper.fuzzyQuery(map));
    }

    @Override
    public RespBean fuzzyQuery(String key) {
        return RespBean.success(materialMapper.SysFuzzyQuery(key));
    }

    @Override
    public RespBean getAllMaterial() {
        return RespBean.success(materialMapper.getAllMaterial());
    }

    @Override
    public RespBean updateMaterialInfo(BaseMaterialVo baseMaterial) {
        if(materialMapper.updateMaterialInfo(baseMaterial)<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"更新失败");
        }
        return RespBean.success();
    }

    @Override
    public RespBean addOne(BaseMaterialVo baseMaterialVo,String companyName) {
        //
        if(materialMapper.addOne(baseMaterialVo)<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"更新失败");
        }
        if(!companyName.equals("系统管理员组织（root）")){
            int materialId=baseMaterialVo.getId();
            CompanyMaterial companyMaterial=new CompanyMaterial();
            companyMaterial.setStock(baseMaterialVo.getStock());
            companyMaterial.setMaterialId(materialId);
            companyMaterial.setCompanyName(companyName);
            if( companyMaterialMapper.addOne(companyMaterial)<1){
                return RespBean.error(RespBeanEnum.UPDATE_ERROR,"更新失败");
            }
        }
        return RespBean.success();
    }

    @Override
    public RespBean getAll(String companyName) {
        return RespBean.success(materialMapper.getAllMaterialByCompanyName(companyName));
    }

    @Override
    public RespBean getAllByCatalog(String companyName, Integer catalogId) {
        return RespBean.success(materialMapper.getAllByCatalog(companyName,catalogId));
    }

    @Override
    public RespBean getApplicableMaterialByWareHouseName(ApplicableByWareHouseNameVo applicable) {
        return RespBean.success(materialMapper.getApplicableMaterialByWareHouseName(applicable));
    }

    @Override
    public RespBean updateMaterial(UpdateMaterialVo materialVo, String companyName) {
        //修改物资
        int row =companyMaterialMapper.updateMaterial(materialVo.getMaterialId(),materialVo.getNumber(),materialVo.getStatus(),companyName);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"修改失败！");
        }
        return RespBean.success();
    }

    @Override
    public RespBean getMaterialStatistics(String companyName) {
        List<StatisticsDTO> list=materialMapper.getMaterialStatistics(companyName);
        //将null设为0
        try {
            for(int i=0;i<list.size();i++){
                if (null==list.get(i).getY()){
                    list.set(i,new StatisticsDTO(list.get(i).getX(),0));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return RespBean.success(list);
    }
}
